package com.hanson.demo.service;

import org.springframework.stereotype.Service;

import com.hanson.demo.actors.ActorRef;
import com.hanson.demo.actors.Message;
import com.hanson.demo.actors.biz_actor.MyOrderActor;
import com.hanson.demo.actors.biz_actor.MyPersonActor;
import com.hanson.demo.actors.system.ActorSystem;

import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
 * 将收到消息队列，在业务处理时跟Actor结合使用
 * @author laich
 *
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class UserActorService {
	
	private ActorSystem system = new ActorSystem();
	
	ActorRef actorOrder = system.createActor("actorOrder", () -> new MyOrderActor(null));
	ActorRef actorPerson = system.createActor("actorPerson", () -> new MyPersonActor(null));

	/**
	 * 将收到消息队列，在业务处理时跟Actor结合使用
	 * @param topic
	 * @param msg
	 */
	public void dealQueueConsumer(String topic, String msg) {
		//log.info("==>dealQueueConsumer:topic{},msg:{}", topic, msg);
		//使用Actor 处理业务消息
		if(topic.equals("hanson")) {
			actorOrder.tell(JSONUtil.toBean(msg, Message.class));
		}else {
			actorPerson.tell(JSONUtil.toBean(msg, Message.class));
		}
	}
}
